package cn.edu.qzuie.artist.module.td.controller.admin.teacher;

import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


import java.util.*;
import java.io.IOException;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.CommonResult;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;
import static cn.edu.qzuie.artist.framework.common.pojo.CommonResult.success;

import cn.edu.qzuie.artist.framework.excel.core.util.ExcelUtils;

import cn.edu.qzuie.artist.framework.apilog.core.annotation.ApiAccessLog;
import static cn.edu.qzuie.artist.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.edu.qzuie.artist.module.td.controller.admin.teacher.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.teacher.TeacherDO;
import cn.edu.qzuie.artist.module.td.service.teacher.TeacherService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - TD教师")
@RestController
@RequestMapping("/td/teacher")
@Validated
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    @PostMapping("/create")
    @Operation(summary = "创建教师")
    @PreAuthorize("@ss.hasPermission('td:teacher:create')")
    public CommonResult<Long> createTeacher(@Valid @RequestBody TeacherSaveReqVO createReqVO) {
        return success(teacherService.createTeacher(createReqVO));
    }


    @PutMapping("/update")
    @Operation(summary = "更新教师")
    @PreAuthorize("@ss.hasPermission('td:teacher:update')")
    public CommonResult<Boolean> updateTeacher(@Valid @RequestBody TeacherSaveReqVO updateReqVO) {
        teacherService.updateTeacher(updateReqVO);
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除教师")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('td:teacher:delete')")
    public CommonResult<Boolean> deleteTeacher(@RequestParam("id") Long id) {
        teacherService.deleteTeacher(id);
        return success(true);
    }



    @GetMapping("/get")
    @Operation(summary = "获得教师")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('td:teacher:query')")
    public CommonResult<TeacherRespVO> getTeacher(@RequestParam("id") Long id) {
        TeacherDO teacher = teacherService.getTeacher(id);
        return success(BeanUtils.toBean(teacher, TeacherRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得教师分页")
    @PreAuthorize("@ss.hasPermission('td:teacher:query')")
    public CommonResult<PageResult<TeacherRespVO>> getTeacherPage(@Valid TeacherPageReqVO pageReqVO) {
        PageResult<TeacherDO> pageResult = teacherService.getTeacherPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TeacherRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出教师 Excel")
    @PreAuthorize("@ss.hasPermission('td:teacher:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTeacherExcel(@Valid TeacherPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TeacherDO> list = teacherService.getTeacherPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "教师.xls", "数据", TeacherRespVO.class,
                        BeanUtils.toBean(list, TeacherRespVO.class));
    }


    @GetMapping("/export-excel-template")
    @Operation(summary = "导出教师 Excel 模板")
    @PreAuthorize("@ss.hasPermission('td:teacher:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTeacherExcelTemplate(HttpServletResponse response) throws IOException {
        // 创建一个空的 TeacherRespVO 列表作为模板
        List<TeacherRespVO> templateList = Collections.emptyList();

        // 导出 Excel 模板
        ExcelUtils.write(response, "教师模板.xls", "数据", TeacherRespVO.class, templateList);
    }


    @PostMapping("/batch-import")
    @Operation(summary = "导入教师")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('td:teacher:export')")
    public CommonResult<TeacherExcelRespVo> batchImportTeachers(@RequestParam("file") MultipartFile file,
                                                     @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport
                                                     ) throws Exception {
        List<TeacherExcelDataInputVO> teacherList = ExcelUtils.read(file, TeacherExcelDataInputVO.class);
        return success(teacherService.batchInsertTeachers(teacherList,updateSupport));
    }

}